package com.eshop.modules.mp.config;

import com.google.common.collect.Maps;
import me.chanjar.weixin.common.error.WxErrorException;
import cn.binarywang.wx.miniapp.message.WxMaXmlOutMessage;
import me.chanjar.weixin.common.session.WxSessionManager;
import cn.binarywang.wx.miniapp.bean.WxMaMessage;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import com.eshop.utils.RedisUtil;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.eshop.utils.ShopKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
import com.eshop.utils.RedisUtils;
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
import cn.binarywang.wx.miniapp.api.WxMaService;
import java.util.Map;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class WxMaConfiguration
{
    private static Map<String, WxMaService> maServices;
    private static Map<String, WxMaMessageRouter> routers;
    private static RedisUtils redisUtils;
    private static WxMaMessageHandler wxMaMessageHandler;
    private static final WxMaMessageHandler BINDSTATECHANGE_HANDLER;
    
    public static WxMaMessageRouter getRouter(final String appid) {
        return WxMaConfiguration.routers.get(appid);
    }
    
    @Autowired
    public WxMaConfiguration(final RedisUtils redisUtils) {
        WxMaConfiguration.redisUtils = redisUtils;
    }
    
    public static WxMaService getWxMaService() {
        WxMaService wxMaService = WxMaConfiguration.maServices.get(ShopKeyUtils.getYshopWeiXinMaSevice());
        if (wxMaService == null || WxMaConfiguration.redisUtils.get(ShopKeyUtils.getYshopWeiXinMaSevice()) == null) {
            final WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
            config.setMsgDataFormat("JSON");
            config.setAppid((String)RedisUtil.get(ShopKeyUtils.getWxAppAppId()));
            config.setSecret((String)RedisUtil.get(ShopKeyUtils.getWxAppSecret()));
            config.setToken((String)RedisUtil.get(ShopKeyUtils.getWechatMaToken()));
            config.setAesKey((String)RedisUtil.get(ShopKeyUtils.getWechatMaEncodingAESKey()));
            wxMaService = (WxMaService)new WxMaServiceImpl();
            wxMaService.setWxMaConfig((WxMaConfig)config);
            WxMaConfiguration.maServices.put(ShopKeyUtils.getYshopWeiXinMaSevice(), wxMaService);
            WxMaConfiguration.routers.put(ShopKeyUtils.getYshopWeiXinMaSevice(), newRouter(wxMaService));
            WxMaConfiguration.redisUtils.set(ShopKeyUtils.getYshopWeiXinMaSevice(), (Object)"yshop");
        }
        return wxMaService;
    }
    
    public static void removeWxMaService() {
        WxMaConfiguration.redisUtils.del(new String[] { ShopKeyUtils.getYshopWeiXinMaSevice() });
        WxMaConfiguration.maServices.remove(ShopKeyUtils.getYshopWeiXinMaSevice());
        WxMaConfiguration.routers.remove(ShopKeyUtils.getYshopWeiXinMaSevice());
    }
    
    private static WxMaMessageRouter newRouter(final WxMaService service) {
        final WxMaMessageRouter router = new WxMaMessageRouter(service);
        router.rule().handler(WxMaConfiguration.wxMaMessageHandler).next().rule().async(false).msgType("event").event("bindstatechange").handler(WxMaConfiguration.BINDSTATECHANGE_HANDLER).end();
        return router;
    }
    
    static {
        WxMaConfiguration.maServices = Maps.newHashMap();
        WxMaConfiguration.routers = Maps.newHashMap();
        BINDSTATECHANGE_HANDLER = ((wxMessage, context, service, sessionManager) -> {
            wxMessage.getFromUser();
            wxMessage.getContent();
            return null;
        });
    }
}
